﻿Imports System.Data.SqlClient
Module Cmd

    Public Function GetConnection() As SqlConnection
        Dim cn As SqlConnection
        cn = New SqlConnection()
        cn.ConnectionString = "Server=.\sa;user=sa;pwd=123;database=HR_DB;"
        cn.Open()
        Return cn
    End Function

    Public Sub ExecuteNonquery(ByVal sql As String)

        Dim cmd As SqlCommand
        cmd = New SqlCommand()
        cmd.Connection = GetConnection()
        cmd.CommandText = sql

        cmd.ExecuteNonQuery()
    End Sub

    Public Function ExecuteScalar(ByVal sql As String) As Object

        Dim cmd As SqlCommand
        cmd = New SqlCommand()
        cmd.Connection = GetConnection()
        cmd.CommandText = sql
        Dim val = cmd.ExecuteScalar()
        Return val

    End Function

    Public Function ExecuteDataTable(ByVal sql As String) As DataTable
      
        Dim cmd As SqlCommand
        cmd = New SqlCommand()
        cmd.Connection = GetConnection()
        cmd.CommandText = sql

        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()

        Dim dt As DataTable
        dt = New DataTable()
        dt.Load(dr)
        dr.Close()

        Return dt
    End Function
 
End Module
